package com.bayihouse.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bayihouse.annotation.AuthCheck;
import com.bayihouse.common.BaseResponse;
import com.bayihouse.common.DeleteRequest;
import com.bayihouse.common.ErrorCode;
import com.bayihouse.common.ResultUtils;
import com.bayihouse.constant.AdministerConstant;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.exception.ThrowUtils;
import com.bayihouse.model.entity.Unit;
import com.bayihouse.model.request.unit.UnitAddRequest;
import com.bayihouse.model.request.unit.UnitQueryRequest;
import com.bayihouse.model.request.unit.UnitUpdateRequest;
import com.bayihouse.model.vo.UnitVO;
import com.bayihouse.service.UnitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/unit")
@Slf4j
public class UnitController {

    @Resource
    private UnitService unitService;


    // endregion

    // region 增删改查

    /**
     * 创建用户
     *
     * @param unitAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = AdministerConstant.ADMIN_ROLE)
    public BaseResponse<Long> addUnit(@RequestBody UnitAddRequest unitAddRequest, HttpServletRequest request) {
        if (unitAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Unit unit = new Unit();
        BeanUtils.copyProperties(unitAddRequest, unit);
        boolean result = unitService.save(unit);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(unit.getId());
    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = AdministerConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUnit(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = unitService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户
     *
     * @param unitUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = AdministerConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUnit(@RequestBody UnitUpdateRequest unitUpdateRequest,
                                             HttpServletRequest request) {
        System.out.println(unitUpdateRequest);
        if (unitUpdateRequest == null || unitUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Unit unit = new Unit();
        BeanUtils.copyProperties(unitUpdateRequest, unit);
        boolean result = unitService.updateById(unit);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<UnitVO> getUnitById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Unit unit = unitService.getById(id);
        ThrowUtils.throwIf(unit == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(UnitVO.objToVO(unit));
    }




    /**
     * 分页获取用户列表（仅管理员）
     *
     * @param unitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Page<Unit>> listUnitByPage(@RequestBody UnitQueryRequest unitQueryRequest,
                                                     HttpServletRequest request) {
        long current = unitQueryRequest.getCurrent();
        long size = unitQueryRequest.getPageSize();
        Page<Unit> unitPage = unitService.page(new Page<>(current, size),
                unitService.getQueryWrapper(unitQueryRequest));
        return ResultUtils.success(unitPage);
    }

    /**
     * 分页获取用户封装列表
     *
     * @param unitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Page<UnitVO>> listUnitVOByPage(@RequestBody UnitQueryRequest unitQueryRequest,
                                                         HttpServletRequest request) {
        if (unitQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = unitQueryRequest.getCurrent();
        long size = unitQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Unit> unitPage = unitService.page(new Page<>(current, size),
                unitService.getQueryWrapper(unitQueryRequest));
        System.out.println(unitPage.getRecords());
        Page<UnitVO> unitVOPage = new Page<>(current, size, unitPage.getTotal());
        List<UnitVO> unitVO = unitService.getUnitVOList(unitPage.getRecords());
        unitVOPage.setRecords(unitVO);
        System.out.println(unitVOPage.getRecords());
        return ResultUtils.success(unitVOPage);
    }

}
